using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EM.Repository.utility;
using Telerik.WebControls;
using System.Data.SqlClient;

namespace EM.Admin.OnlineUser
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int PageId = 10;
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(PageId, Authentication.RedirectionURLTypes.LoginPage);
        }

        private DataTable GetData()
        {
            string sSQL = "";
            sSQL += " SELECT S.USERID, U.USERDESC, U.USEREMAIL, SH.IPADDRESS, S.SESSIONHEADERID ";
            sSQL += " from TBLACTIVESESSION S, TBLUSER U, TBLSESSIONHEADER SH  ";
            sSQL += " WHERE S.USERID = U.USERID  ";
            sSQL += " AND S.SESSIONHEADERID = SH.SESSIONHEADERID AND S.WORKGROUPID = " + Session["WORKGROUPID"];
            sSQL += " ORDER BY U.USERDESC";  

            DataTable dt = new DataTable();
            DbHelper dsrv = new DbHelper();

            dt = dsrv.GetDataTable(sSQL, "TBL");
            return dt;
        }

        protected void rgMaster_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = GetData();
            rgMaster.DataSource = dt;
        }

        protected void rgMaster_ItemCommand(object source, GridCommandEventArgs e)
        {
            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();
            string sSQL = "";

            if (e.CommandName == "SessionEndNow")
            {
                cmd = new SqlCommand();

                sSQL += " 	BEGIN TRANSACTION ";
                sSQL += " 	UPDATE TBLSESSIONHEADER ";
                sSQL += " 	SET ENDDATE = GETDATE() ";
                sSQL += " 	WHERE USERID = @USERID";
                sSQL += " 	IF @@ERROR <>  0 ";
                sSQL += " 	BEGIN ";
                sSQL += " 		ROLLBACK TRANSACTION ";
                sSQL += " 		RETURN ";
                sSQL += " 	END 	 ";
                sSQL += " 	DELETE FROM TBLACTIVESESSION ";
                sSQL += " 	WHERE USERID = @USERID";
                sSQL += " 	IF @@ERROR <>  0 ";
                sSQL += " 	BEGIN ";
                sSQL += " 		ROLLBACK TRANSACTION ";
                sSQL += " 		RETURN ";
                sSQL += " 	END 	 ";
                sSQL += " 	COMMIT TRANSACTION ";

                cmd.CommandText = sSQL;
                cmd.Parameters.Add("@USERID", SqlDbType.Int).Value = Convert.ToInt32(rgMaster.Items[e.Item.ItemIndex].Cells[2].Text);

                srv.ExecuteNonQuery(cmd);
                rgMaster.Rebind();
            }
        }

        protected void rgMaster_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Header)
            {
            }

            if (e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem || e.Item.ItemType == Telerik.WebControls.GridItemType.Item)
            {
            }

            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Pager)
            {
                Label lblRecordCount = new Label();
                e.Item.Controls[0].Controls.AddAt(0, lblRecordCount);
                lblRecordCount.Text = rgMaster.StatusBarSettings.ReadyText + " | sayfaya git" + " : ";
                Telerik.WebControls.RadGrid RadGrid_ = ((Telerik.WebControls.RadGrid)e.Item.Parent.NamingContainer.Parent);
                if (RadGrid_.PageCount > 1)
                {
                    RadGrid_.ShowStatusBar = false;
                }
            }
        }

    }
}
